package annotation.action;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author: Hevery
 * @Description:
 * @Date: Created on 2017/9/12
 * @Modified By:
 */
public class Test {
    public static void main(String[] args) {
        User u1 = new User();
        u1.setId(10);//查询Id为10的用户

        User u2 = new User();
        u2.setUserName("Lucy");//查询用户名为Lucy的用户
        u2.setAge(20);

        User u3 = new User();
        u3.setEmail("Hevery@souhu.com");//查询邮箱为其中任意一个的用户

        Department dep = new Department();
        dep.setId(1);
        dep.setName("质量部");

        String sql1 = query(u1);
        String sql2 = query(u2);
        String sql3 = query(u3);
        String sql4 = query(dep);

        System.out.println(sql1);
        System.out.println(sql2);
        System.out.println(sql3);
        System.out.println(sql4);
    }
    private static String query(Object obj) {
        StringBuilder buffer = new StringBuilder();
        //1.获取到Class
        Class c = obj.getClass();
        //2.获取table的名字
        boolean exists = c.isAnnotationPresent(Table.class);
        if(!exists){
            return null;
        }
        Table table = (Table)c.getAnnotation(Table.class);
        String tableName = table.value();
        buffer.append("select * from ").append(tableName).append(" where 1=1");
        //3.遍历所有的字段
        Field[] fields = c.getDeclaredFields();
        for(Field f : fields){
            //4.处理每个字段对应的SQL
            //4.1拿到字段的名字
            boolean fexists = f.isAnnotationPresent(Column.class);
            if(!fexists){
                continue;
            }

            Column column = f.getAnnotation(Column.class);
            String columnName = column.value();
            //4.2拿到字段值
            Object filedValue = null;
            String filedName = f.getName();
            String getMethodName = "get"+ filedName.substring(0,1).toUpperCase()+filedName.substring(1);
            try {
                Method getMethod = c.getMethod(getMethodName);
                filedValue = (Object)getMethod.invoke(obj);

            } catch (Exception e) {
                e.printStackTrace();
            }
            //4.3拼装SQL
            if(null == filedValue || filedValue instanceof Integer && (Integer)filedValue == 0){
                continue;
            }
            buffer.append(" and ").append(filedName).append("=");
            if(filedValue instanceof String ){
                if(((String) filedValue).contains(",")){
                    buffer.append("in(");
                    String[] values = ((String) filedValue).split(",");
                    for(String v : values){
                        buffer.append("'").append(v).append("'").append(",");
                    }
                    buffer.deleteCharAt(buffer.length()-1);
                    buffer.append(")");
                }else{
                    buffer.append("'").append(filedValue).append("'");
                }

            }else if(filedValue instanceof Integer){
                buffer.append(filedValue);
            }

        }

        return buffer.toString();

    }


}
